/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package name.coolwind.cxblog.web;

import javax.servlet.http.HttpServletRequest;
import name.coolwind.cxblog.service.BlogService;
import name.coolwind.cxblog.service.UserService;
import name.coolwind.cxblog.util.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author coolwind
 */
@Controller
@RequestMapping(value = "/common")
public class CommonController {

    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;
    
    /**
     *
     * @param userId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/header")
    public String headerPage(@CookieValue(value = "user_id", required = false) String userId, ModelMap modelMap, HttpServletRequest req) {
        if (userId != null) {
            String userName = userService.getUserNameByUserId(userId);
            modelMap.addAttribute("user_id", userId);
            modelMap.addAttribute("user_name", userName);
        }
        return "common/header";
    }

    @RequestMapping(value = "/footer")
    public String footerPage() {
        return "common/footer";
    }
    
    @RequestMapping(value = "/pagination/{total}/{page}")
    public String paginationPage(@PathVariable String total, @PathVariable String page, ModelMap model){
        Pagination p = new Pagination(Integer.valueOf(total), Integer.valueOf(page), /*blogService.getPerPage()*/ 20, 5);
        int start = p.getStart();
        int end = p.getEnd();
        int prevPage = p.getPrevPage();
        int nextPage = p.getNextpage();
        int numPages = p.getNumPages();
        if(numPages > 1){
            model.put("start", start);
            model.put("end", end);
            model.put("prevPage", prevPage);
            model.put("nextPage", nextPage);
            model.put("curPage", Integer.valueOf(page));
        }
        
        return "common/pagination";
    }
}
